package com.cpit.platform.security.service;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken;  
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;  
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.cpit.platform.common.constant.SystemConstant;
import com.cpit.platform.common.constant.UserConstant;
import com.cpit.platform.dao.RoleRepository;
import com.cpit.platform.dao.UserToRoleRepository;
import com.cpit.platform.dto.Authority;
import com.cpit.platform.dto.Role;
import com.cpit.platform.dto.User;
import com.cpit.platform.dto.UserToRole;
import com.cpit.platform.log.LogOperator;
import com.cpit.platform.service.AuthorityService;
import com.cpit.platform.service.UserService;  



public class CustomUserDetailsServiceCas /* 
//实现UserDetailsService接口，实现loadUserByUsername方法 
implements UserDetailsService { 
@Override 
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 
    System.out.println("当前的用户名是："+username); 
    //这里我为了方便，就直接返回一个用户信息，实际当中这里修改为查询数据库或者调用服务什么的来获取用户信息 
    UserInfo userInfo = new UserInfo(); 
    userInfo.setUsername("admin"); 
    userInfo.setName("admin"); 
    Set<AuthorityInfo> authorities = new HashSet<AuthorityInfo>(); 
    AuthorityInfo authorityInfo = new AuthorityInfo("TEST"); 
    authorities.add(authorityInfo); 
    userInfo.setAuthorities(authorities); 
    return userInfo; 
}*/


//实现AuthenticationUserDetailsService，实现loadUserDetails方法  
implements AuthenticationUserDetailsService<CasAssertionAuthenticationToken> {  
	@Autowired 
	private UserService userService; 
	
	@Autowired
	private AuthorityService authorityService;
	
	@Autowired
	private UserToRoleRepository userToRoleRepository;
	
	@Autowired
	private RoleRepository roleRepository;
	
	private final String loggerName = this.getClass().getName();
	
	@Override  
	public UserDetails loadUserDetails(CasAssertionAuthenticationToken token) 
			throws UsernameNotFoundException {  
		System.out.println("当前的用户名是：" + token.getName());
		if(token.getName().equals("admin")) {
			LogOperator.debug(loggerName, "current user is: admin");
			User adminUser = new User();
			User uTmp = userService.findByUsername(token.getName());
			if(uTmp == null)
				adminUser.setId(1);
			else
				adminUser.setId(uTmp.getId());
			adminUser.setUsername("admin");
			adminUser.setDisplayName(uTmp.getDisplayName());
			adminUser.setAuthType(UserConstant.USER_AUTH_TYPE_SYSTE_ADMIN);
			adminUser.setOrganizationId(SystemConstant.ORGANIZATION_ID_ROOT);
			adminUser.setOrganizationCode(SystemConstant.ORGANIZATION_CODE_ROOT);
			List<Authority> allAuths = authorityService.findAll();
			UserInfo userInfo = new UserInfo(adminUser);  
			userInfo.setUsername("admin");  
			userInfo.setName("admin");
			Set<AuthorityInfo> authorities = new HashSet<AuthorityInfo>();
			if(allAuths != null && allAuths.size() > 0) {
				for(Authority a : allAuths) {
					AuthorityInfo authorityInfo = new AuthorityInfo(a.getAuthorityContent()); 
					authorities.add(authorityInfo);  
				}
			}
			userInfo.setAuthorities(authorities);  
			return userInfo;  
		} else {
			User uTmp = userService.findByUsername(token.getName());
			UserInfo userInfo = new UserInfo(uTmp);
			Set<AuthorityInfo> authorities = new HashSet<AuthorityInfo>();
			List<UserToRole> allRoles = userToRoleRepository.findByUserId(uTmp.getId());
			if(allRoles != null && allRoles.size() > 0) {
				for(UserToRole rr : allRoles) {
					Role r = roleRepository.findById(rr.getRoleId());
					List<Authority> allAuths = r.getAuthorities();
					if(allAuths != null && allAuths.size() > 0) {
						for(Authority a : allAuths) {
							AuthorityInfo authorityInfo = new AuthorityInfo(a.getAuthorityContent()); 
							authorities.add(authorityInfo);  
						}
					}
				}
			}
			userInfo.setAuthorities(authorities);  
			return userInfo;
		}
		
	}
	
}